package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.fire.matterManagement.dto.ToDoListDto;
import com.dykj.fireFighting.api.fire.matterManagement.entity.ToDoList;
import com.dykj.fireFighting.api.fire.matterManagement.service.IToDoListService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;

/**
 * @Author: zly
 * @Description : 待办事项
 * @Date: 2021-09-01
 */
@Slf4j
@Api(value = "维保app待办事项接口", tags = {"维保app待办事项接口"})
@RestController
@RequestMapping("app/toDoList")
public class AppToDoListController {
    @DubboReference
    private IToDoListService toDoListService;


    /**
     * 维保app待办事项列表
     *
     * @return
     */
    @AutoLog(value = "维保app待办事项-分页列表查询")
    @ApiOperation(value = "维保app待办事项分页列表", notes = "维保app待办事项分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<ToDoList>> treeGrid(ToDoListDto toDoListDto) {
        LoginUserVo user = ShiroUtil.getUser();
        toDoListDto.setUnitId(user.getEnterpriseId().toString());

        PageVo<ToDoList> page = toDoListService.queryList(toDoListDto);
        return CommonResult.success(page);
    }

    /**
     * 获取维保app待办事项信息
     */
    @AutoLog(value = "维保app待办事项-获取维保app待办事项信息")
    @ApiOperation(value = "获取维保app待办事项信息", notes = "获取维保app待办事项信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<ToDoList> info(@PathVariable("id") String id){
        ToDoList toDoList = toDoListService.getById(id);
        return CommonResult.success(toDoList);
    }

    /**
     * 保存维保app待办事项信息
     */
    @AutoLog(value = "维保app待办事项-保存维保app待办事项信息")
    @ApiOperation(value = "保存维保app待办事项信息", notes = "保存维保app待办事项信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody ToDoList toDoList) {
        try {
            toDoList.setId(UUIDUtils.get32UUID());
            LoginUserVo user = ShiroUtil.getUser();
            toDoList.setCreateBy(user.getId().toString());
            toDoList.setCreateByName(user.getLoginName());
            toDoList.setUnitId(user.getEnterpriseId().toString());
            toDoList.setCreateDate(new Date());
            toDoListService.save(toDoList);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改维保app待办事项信息
     */
    @AutoLog(value = "维保app待办事项-修改维保app待办事项信息")
    @ApiOperation(value = "修改维保app待办事项信息", notes = "修改维保app待办事项信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody ToDoList toDoList) {
        try {

            toDoListService.updateById(toDoList);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除维保app待办事项信息
     */
    @AutoLog(value = "维保app待办事项-删除维保app待办事项信息")
    @ApiOperation(value = "删除维保app待办事项信息", notes = "删除维保app待办事项信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            toDoListService.deleteByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

}
